package com.rongj.generation.core;

import com.rongj.generation.beetl.BeetlUtil;
import com.rongj.generation.core.util.EntityDto;
import com.rongj.generation.core.util.EntityUtils;
import com.rongj.generation.core.util.FileUtil;
import com.rongj.generation.core.util.Propertys;
import com.rongj.generation.data.DatabaseUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenerationHandle {

    public void execute(String tableName,String basePackage,String entityName,String entityPackage, boolean hibernate){

        List<String> columnNames = DatabaseUtil.getColumn(tableName);
        List<String> columnTypes = DatabaseUtil.getColumnType(tableName);
        System.out.println(columnTypes.toString());
        createEntity(columnNames,columnTypes,basePackage,entityName,tableName,entityPackage,hibernate);
        createRepositoryImpl(basePackage,entityName);
    }

    protected void createEntity(List<String> columnNames,List<String> columntypes,String basePackage,String entityName,String tableName,String entityPackage,boolean hibernate){
        String basefile = basePackage.replaceAll("\\.","/");
        String fileName =basefile+ File.separator+ Propertys.getKey("entity-path")+File.separator+entityName;
        String basePath = FileUtil.getSysPath();
        FileUtil.mkdirFolder(basePath,fileName);
        List<EntityDto> listDto = EntityUtils.analyze(columnNames,columntypes,hibernate);
        Map<String,Object>  map = new HashMap<String,Object>();
        map.put("basePackage",basePackage);
        map.put("codePackage",Propertys.getKey("entity-path").replaceAll("/",""));
        map.put("entityPackage",entityPackage);
        map.put("entityName",entityName);
        map.put("tableName",tableName);
        map.put("entityDtoList",listDto);
        map.put("hibernate",hibernate);

        File entityFile = new File(basePath+File.separator+fileName+File.separator+entityName+".java");
        BeetlUtil.outputTemplate(map,"entity.java",entityFile);

        File repositoryInterface = new File(basePath+File.separator+fileName+File.separator+entityName+"Repository.java");
        BeetlUtil.outputTemplate(map,"repository.java",repositoryInterface);
    }

    protected void createRepositoryImpl(String basePackage,String entityName){
        String basefile = basePackage.replaceAll("\\.","/");
        String fileName =basefile+ File.separator+ Propertys.getKey("repository-path");
        String basePath = FileUtil.getSysPath();
        FileUtil.mkdirFolder(basePath,fileName);

        Map<String,Object>  map = new HashMap<String,Object>();
        map.put("basePackage",basePackage);
        map.put("codePackage",Propertys.getKey("repository-path").replaceAll("/",""));
        map.put("entityName",entityName);

        File repositoryImpl = new File(basePath+File.separator+fileName+File.separator+entityName+"RepositoryImpl.java");
        BeetlUtil.outputTemplate(map,"repositoryImpl.java",repositoryImpl);
    }

}
